package guiTesting;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Frame;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JToolBar;
import net.miginfocom.swing.MigLayout;
import javax.swing.JMenuItem;
import java.awt.Toolkit;

import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.ImageIcon;
import javax.swing.JSeparator;
import javax.swing.JButton;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Arrays;
import java.util.List;
import java.util.Vector;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.LineBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JSplitPane;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.ListSelectionModel;

import com.atlassian.crowd.exception.ApplicationPermissionException;
import com.atlassian.crowd.exception.InvalidAuthenticationException;
import com.atlassian.crowd.exception.OperationFailedException;
import com.atlassian.crowd.integration.rest.entity.UserEntity;
import com.atlassian.crowd.model.user.User;

public class Main extends JFrame {
	//private SearchInputForm form1;
	private JTable table;
	private JButton btnNewButtonSearch;
	private JTextField textFieldSearchTerm;
	private RetrieveUserInfo userInfo;
	Vector rowData = new Vector();

	static Vector columnNamesV ;
	static String[] columnNames  = { "First Name ", "Last Name","Email" , "Display Name", "Is Active"};
	private JScrollPane scrollPaneSearchResultsTable;

	//private List searchResults;


	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Throwable e) {
			e.printStackTrace();
		}
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Main frame = new Main();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Main() {
		initiateComponent();
		createEvents();
	}


	private void initiateComponent(){
		setIconImage(Toolkit.getDefaultToolkit().getImage(Main.class.getResource("/resources/logo.gif")));
		setTitle("UserInfo");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 697, 378);
		
//JMenu Bar items 
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);

		JMenu mnFile = new JMenu("File");
		menuBar.add(mnFile);

		JMenuItem mntmOpen = new JMenuItem("Open");
		mnFile.add(mntmOpen);

		JMenuItem mntmSave = new JMenuItem("Save");
		mnFile.add(mntmSave);

		JMenuItem mntmClose = new JMenuItem("Close");
		mnFile.add(mntmClose);

		JMenuItem mntmExitProgram = new JMenuItem("Exit Program");
		mntmExitProgram.setIcon(new ImageIcon(Main.class.getResource("/resources/exit.png")));
		mnFile.add(mntmExitProgram);

		JMenu mnEdit = new JMenu("Edit");
		menuBar.add(mnEdit);

		JMenuItem mntmCopy = new JMenuItem("Copy");
		mnEdit.add(mntmCopy);

		JMenuItem mntmPaste = new JMenuItem("Paste");
		mnEdit.add(mntmPaste);

		JMenuItem mntmCut = new JMenuItem("Cut");
		mnEdit.add(mntmCut);

		JSeparator separator = new JSeparator();
		mnEdit.add(separator);

		JMenuItem mntmUndo = new JMenuItem("Undo");
		mnEdit.add(mntmUndo);

		JMenuItem mntmRedo = new JMenuItem("Redo");
		mnEdit.add(mntmRedo);

		JMenu mnHelp = new JMenu("Help");
		menuBar.add(mnHelp);

		JMenuItem mntmReadMoreIn = new JMenuItem("Read More in Brain");
		mnHelp.add(mntmReadMoreIn);

		JMenu mnAbout = new JMenu("About");
		menuBar.add(mnAbout);

		JMenuItem mntmProfilemgt = new JMenuItem("Profile-MGT");
		mntmProfilemgt.setIcon(new ImageIcon(Main.class.getResource("/resources/information.png")));
		mnAbout.add(mntmProfilemgt);

		
		//Main content jpanels
		JPanel mainContentPanel = new JPanel();
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(mainContentPanel, GroupLayout.DEFAULT_SIZE, 667, Short.MAX_VALUE)
					.addContainerGap())
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addComponent(mainContentPanel, GroupLayout.PREFERRED_SIZE, 314, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(54, Short.MAX_VALUE))
		);

		//search input jpanel 
		
		JPanel panelSearchInput = new JPanel();
		panelSearchInput.setBorder(new TitledBorder(null, "Search", TitledBorder.LEADING, TitledBorder.TOP, null, Color.LIGHT_GRAY));
		
		scrollPaneSearchResultsTable = new JScrollPane();
		Dimension minimumSize = new Dimension(500, 400);
		scrollPaneSearchResultsTable.setMinimumSize(minimumSize);
		GroupLayout gl_mainContentPanel = new GroupLayout(mainContentPanel);
		gl_mainContentPanel.setHorizontalGroup(
			gl_mainContentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_mainContentPanel.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_mainContentPanel.createParallelGroup(Alignment.LEADING)
						.addComponent(scrollPaneSearchResultsTable, GroupLayout.PREFERRED_SIZE, 647, GroupLayout.PREFERRED_SIZE)
						.addComponent(panelSearchInput, GroupLayout.PREFERRED_SIZE, 570, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(86, Short.MAX_VALUE))
		);
		gl_mainContentPanel.setVerticalGroup(
			gl_mainContentPanel.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_mainContentPanel.createSequentialGroup()
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
					.addComponent(panelSearchInput, GroupLayout.PREFERRED_SIZE, 130, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(scrollPaneSearchResultsTable, GroupLayout.PREFERRED_SIZE, 146, GroupLayout.PREFERRED_SIZE)
					.addGap(73))
		);


		JLabel lblSearchInstr = new JLabel("Search");

		textFieldSearchTerm = new JTextField();

		btnNewButtonSearch = new JButton("Search");


		JButton btnCancelSearch = new JButton("Cancel Search");
		GroupLayout gl_panelSearchInput = new GroupLayout(panelSearchInput);
		gl_panelSearchInput.setHorizontalGroup(
				gl_panelSearchInput.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panelSearchInput.createSequentialGroup()
						.addContainerGap()
						.addComponent(lblSearchInstr, GroupLayout.PREFERRED_SIZE, 86, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addGroup(gl_panelSearchInput.createParallelGroup(Alignment.LEADING, false)
								.addGroup(gl_panelSearchInput.createSequentialGroup()
										.addComponent(btnNewButtonSearch, GroupLayout.PREFERRED_SIZE, 141, GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
										.addComponent(btnCancelSearch, GroupLayout.PREFERRED_SIZE, 114, GroupLayout.PREFERRED_SIZE))
										.addComponent(textFieldSearchTerm, GroupLayout.PREFERRED_SIZE, 326, GroupLayout.PREFERRED_SIZE))
										.addContainerGap(132, Short.MAX_VALUE))
		);
		//search input jpanel layout - grouplayout
		gl_panelSearchInput.setVerticalGroup(
				gl_panelSearchInput.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panelSearchInput.createSequentialGroup()
						.addContainerGap()
						.addGroup(gl_panelSearchInput.createParallelGroup(Alignment.BASELINE)
								.addComponent(lblSearchInstr, GroupLayout.PREFERRED_SIZE, 24, GroupLayout.PREFERRED_SIZE)
								.addComponent(textFieldSearchTerm, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE))
								.addGap(18)
								.addGroup(gl_panelSearchInput.createParallelGroup(Alignment.BASELINE)
										.addComponent(btnNewButtonSearch, GroupLayout.PREFERRED_SIZE, 34, GroupLayout.PREFERRED_SIZE)
										.addComponent(btnCancelSearch, GroupLayout.PREFERRED_SIZE, 31, GroupLayout.PREFERRED_SIZE))
										.addContainerGap(20, Short.MAX_VALUE))
		);
		panelSearchInput.setLayout(gl_panelSearchInput);
		mainContentPanel.setLayout(gl_mainContentPanel);
		getContentPane().setLayout(groupLayout);

	}
	private void createEvents(){
		btnNewButtonSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (textFieldSearchTerm.getText()!=null){
					try {
						//System.out.println(textFieldSearchTerm.getText());
						userInfo = new RetrieveUserInfo();
						List<User> searchResults = userInfo.searchUsers(textFieldSearchTerm.getText());
						//Vector rowData = new Vector();
						if (searchResults.size()==0){
							JOptionPane.showMessageDialog(null, "No User Found", "Info", JOptionPane.INFORMATION_MESSAGE);
						}
						for (int i=0; i<searchResults.size(); i++){
							User uz =  searchResults.get(i);
							Vector colData = new Vector(Arrays.asList( uz.getFirstName(), uz.getLastName(),uz.getEmailAddress(), uz.getDisplayName(),uz.isActive()));
							rowData.add(colData);
							columnNamesV = new Vector(Arrays.asList(columnNames));
							System.out.println( columnNamesV.get(2));
						}
						table = new JTable(rowData, columnNamesV);
						table.setName("search Results");

						table.setShowGrid(true);
						scrollPaneSearchResultsTable.add(table);
						scrollPaneSearchResultsTable.setColumnHeaderView(table);

						//System.out.println(searchResults);
					} catch (OperationFailedException e) {
						JOptionPane.showMessageDialog(null, "Search Failed", "Error", JOptionPane.ERROR_MESSAGE);

						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (InvalidAuthenticationException e) {
						JOptionPane.showMessageDialog(null, "APP Authentication Failed", "Error", JOptionPane.ERROR_MESSAGE);
						e.printStackTrace();
					} catch (ApplicationPermissionException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

				} else {
					JOptionPane.showMessageDialog(null, "Please enter search term", "Info", JOptionPane.INFORMATION_MESSAGE);
					
				}
			}
		});

	}
}
